package android.pruebas.listview3;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class PaisArrayAdapter extends ArrayAdapter<Pais> {
	private static final String tag = "PaisArrayAdapter";
	private static final String ASSETS_DIR = "images/";
	
	private Context context;
	private Paises paises;

	public PaisArrayAdapter(Context context, int textViewResourceId, Paises paises) {
		super(context, textViewResourceId, paises);
		this.context = context;
		this.paises = paises;
	}

	public int getCount() {
		return this.paises.size();
	}

	public Pais getItem(int index) {
		return this.paises.get(index);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
	
	    ImageView bandera;
		TextView nombre;
		
		if (row == null) {
			// ROW INFLATION
			Log.d(tag, "Starting XML Row Inflation ... ");
			LayoutInflater inflater = (LayoutInflater) this.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.list_item , parent, false);
			Log.d(tag, "Successfully completed XML Row Inflation!");
		}

		// Get item
		Pais pais = getItem(position);

		// Get reference to ImageView 
		bandera = (ImageView) row.findViewById(R.id.bandera);

		// Get reference to TextView 
		nombre = (TextView) row.findViewById(R.id.nombre);


		//Nombre del pais.
		nombre.setText(pais.getNombre());

		// Bandera del pais.
		String imgFilePath = ASSETS_DIR + pais.getBandera();
		try {
			Bitmap bitmap = BitmapFactory.decodeStream(this.context.getResources().getAssets()
					.open(imgFilePath));
			bandera.setImageBitmap(bitmap);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return row;
	}
}